//
//  PPDNAController.swift
//  PP
//
//  Created by 乐同 on 2018/2/27.
//  Copyright © 2018年 乐同. All rights reserved.
//

import UIKit
import SnapKit

class PPDNAController: UIViewController {
    
    let kScreenWidth:CGFloat = UIScreen.main.bounds.size.width;
    let kScreenHeight:CGFloat = UIScreen.main.bounds.size.height;
    let statusBarHeigt:CGFloat = UIApplication.shared.statusBarFrame.height;
    var dnaArr1:NSMutableArray! = NSMutableArray.init();
    var dnaArr2:NSMutableArray! = NSMutableArray.init();
    var resultArray:NSMutableArray! = NSMutableArray.init();
    var flag:Bool = false;
    
//TODO: 覆盖并重写 UIViewController method
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI();
    }
    
//TODO: 私有方法
    private func setupUI() {
        
        let height:CGFloat = (kScreenHeight-statusBarHeigt)/3;
        
        self.view.backgroundColor = UIColor.black;
        
        self.view.addSubview(self.resultView);
        resultView.snp.makeConstraints { (make) in
            make.top.equalTo(self.view.snp.top).offset(statusBarHeigt);
            make.left.right.equalTo(self.view);
            make.height.equalTo(height*2);
        }
        
        resultView.addSubview(self.dnaLabel);
        dnaLabel.snp.makeConstraints { (make) in
            make.top.equalTo(resultView.snp.top).offset(5);
            make.left.equalTo(resultView.snp.left).offset(10);
        }
        
        resultView.addSubview(self.resultLabel);
        resultLabel.snp.makeConstraints { (make) in
            make.top.equalTo(dnaLabel.snp.bottom).offset(5);
            make.right.equalTo(resultView.snp.right).offset(-10);
            make.bottom.equalTo(resultLabel.snp.bottom).offset(-5);
        }
        
        let width:CGFloat = kScreenWidth/4;
        self.view.addSubview(self.touchView);
        touchView.snp.makeConstraints { (make) in
            make.top.equalTo(resultView.snp.bottom);
            make.left.right.bottom.equalTo(self.view);
        }
        
        let dnaWidth:CGFloat = (width*3)/2;
        touchView.addSubview(self.gDnaBtn);
        gDnaBtn.snp.makeConstraints { (make) in
            make.top.equalTo(touchView).offset(10);
            make.left.equalTo(touchView).offset(10);
            make.width.equalTo(dnaWidth-10);
            make.bottom.equalTo(touchView).multipliedBy(0.5).offset(-5);
        }
        
        touchView.addSubview(self.bDnaBtn);
        bDnaBtn.snp.makeConstraints { (make) in
            make.top.equalTo(gDnaBtn);
            make.left.equalTo(gDnaBtn.snp.right).offset(10);
            make.width.equalTo(dnaWidth-10)
            make.bottom.equalTo(touchView).multipliedBy(0.5).offset(-5);
        }
        
        touchView.addSubview(self.yDnaBtn);
        yDnaBtn.snp.makeConstraints { (make) in
            make.top.equalTo(touchView.snp.centerY).offset(5);
            make.left.equalTo(touchView).offset(10);
            make.width.equalTo(dnaWidth-10);
            make.bottom.equalTo(touchView).offset(-10);
        }
        
        touchView.addSubview(self.wDnaBtn);
        wDnaBtn.snp.makeConstraints { (make) in
            make.top.equalTo(yDnaBtn);
            make.left.equalTo(yDnaBtn.snp.right).offset(10);
            make.width.equalTo(dnaWidth-10);
            make.bottom.equalTo(touchView).offset(-10);
        }
        
        touchView.addSubview(self.acBtn);
        acBtn.snp.makeConstraints { (make) in
            make.top.equalTo(bDnaBtn);
            make.left.equalTo(bDnaBtn.snp.right).offset(10);
            make.right.equalTo(touchView).offset(-10);
            make.bottom.equalTo(touchView).multipliedBy(0.3333).offset(-5);
        }

        touchView.addSubview(self.pairBtn);
        touchView.addSubview(self.resultBtn);
        pairBtn.snp.makeConstraints { (make) in
            make.top.equalTo(acBtn.snp.bottom).offset(10);
            make.left.right.equalTo(acBtn);
            make.height.equalTo(touchView).multipliedBy(0.3333).offset(-15);
        }
        resultBtn.snp.makeConstraints { (make) in
            make.top.equalTo(pairBtn.snp.bottom).offset(10);
            make.left.right.equalTo(acBtn);
            make.bottom.equalTo(touchView).offset(-10);
        }
    }
    
    private func randomColor() -> UIColor {
        let red = CGFloat(arc4random()%256)/255.0
        let green = CGFloat(arc4random()%256)/255.0
        let blue = CGFloat(arc4random()%256)/255.0
        return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
    }
    
    private func chooseDNA(dna:String,arr:NSMutableArray){
        if arr.count > 1 {
            arr.replaceObject(at: 1, with: dna);
        }else{
            arr.addObjects(from: [dna,dna]);
        }
    }
    
    @objc private func dnaAction(btn:UIButton) {
        var dna:String?;
        if btn == gDnaBtn{
            dna = "1";
        }else if btn == bDnaBtn{
            dna = "2";
        }else if btn == yDnaBtn{
            dna = "3";
        }else {
            dna = "4";
        }
        if flag == false {
            self.chooseDNA(dna: dna!, arr: dnaArr1);
            dnaLabel.attributedText = self.setDnaTxt(arr: dnaArr1);
        }else {
            self.chooseDNA(dna: dna!, arr: dnaArr2);
            let attr:NSMutableAttributedString = self.setDnaTxt(arr: dnaArr1);
            attr.append(NSMutableAttributedString.init(string: "\n"));
            attr.append(self.setDnaTxt(arr: dnaArr2));
            dnaLabel.attributedText = attr;
        }
    }
    
    @objc private func clearAction() {
        dnaArr1.removeAllObjects();
        dnaArr2.removeAllObjects();
        dnaLabel.attributedText = nil;
        resultLabel.attributedText = nil;
        flag = false;
    }
    
    @objc private func pairAction() {
        flag = true;
    }
    
    @objc private func resultAction() {
        let attr:NSMutableAttributedString = NSMutableAttributedString.init();
        for str1 in dnaArr1{
            for str2 in dnaArr2{
                attr.append(self.setDnaTxt(arr: [str1,str2]));
                attr.append(NSAttributedString.init(string: "\n"));
            }
        }
        self.resultLabel.attributedText = attr;
    }
    
    private func setDnaTxt(arr:NSMutableArray)->NSMutableAttributedString {
        var str:String?;
        var char:String?;
        // 绿色太平洋鹦鹉

        if arr.contains("1") && arr.contains("2"){
            str = "绿(蓝基因)";
            char = "YyBB";
        }else if arr.contains("1") && arr.contains("3"){
            str = "绿(黄基因)";
            char = "YYBb";
        }else if arr.contains("1") && arr.contains("4"){
            str = "绿(白基因)";
            char = "YyBb";
        }else if arr.contains("2") && arr.contains("3"){
            str = "绿(白基因)";
            char = "YyBb";
        }
        //  蓝色太平洋鹦鹉
        else if arr.contains("2") && arr.contains("4"){
            str = "蓝(白基因)";
            char = "yyBb";
        }
        //  黄色太平洋鹦鹉
        else if arr.contains("3") && arr.contains("4"){
            str = "黄(白基因)";
            char = "Yybb";
        }
        else{
            switch (arr.firstObject as! NSString).intValue{
                case 1:
                    str = "绿";
                    char = "YYBB";
                    break;
                case 2:
                    str = "蓝";
                    char = "yyBB";
                    break;
                case 3:
                    str = "黄";
                    char = "YYbb";
                    break;
                case 4:
                    str = "白";
                    char = "yybb";
                    break;
                default:
                    break;
            }
        }
        let strAttr:NSAttributedString = NSAttributedString.init(string: str!, attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 18),
                                                                                             NSAttributedStringKey.foregroundColor:UIColor.white])
        let charAttr:NSAttributedString = NSAttributedString.init(string: char!, attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 10),
                                                                                             NSAttributedStringKey.foregroundColor:UIColor.white])
        let attr:NSMutableAttributedString = NSMutableAttributedString.init(attributedString: strAttr);
        attr.append(charAttr);
        return attr;
    }

//TODO: 懒加载
    lazy var resultView:UIView = {
        let rv:UIView = UIView.init();
        rv.backgroundColor = UIColor.black;
        return rv;
    }()
    
    lazy var touchView:UIView = {
        let tv:UIView = UIView.init();
        tv.backgroundColor = UIColor.black;
        return tv;
    }()
    
    lazy var dnaLabel:UILabel = {
        let dl:UILabel = UILabel.init();
        dl.textColor = UIColor.white;
        dl.numberOfLines = 2;
        return dl;
    }()
    
    lazy var resultLabel:UILabel = {
        let rl:UILabel = UILabel.init();
        rl.textColor = UIColor.white;
        rl.numberOfLines = 0;
        return rl;
    }()
    
    lazy var gDnaBtn:UIButton = {
        let gdb:UIButton = UIButton.init(type: UIButtonType.custom);
        let strAttr:NSAttributedString = NSAttributedString.init(string: "绿", attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 18),
                                                                                             NSAttributedStringKey.foregroundColor:UIColor.white])
        let charAttr:NSAttributedString = NSAttributedString.init(string: "YB", attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 10),
                                                                                             NSAttributedStringKey.foregroundColor:UIColor.white])
        var attr:NSMutableAttributedString = NSMutableAttributedString.init(attributedString: strAttr);
        attr.append(charAttr);
        gdb.setAttributedTitle(attr, for: UIControlState.normal);
        gdb.addTarget(self, action:#selector(self.dnaAction(btn:)), for: UIControlEvents.touchUpInside);
        gdb.backgroundColor = UIColor.init(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0);
        gdb.layer.cornerRadius = 5;
        return gdb;
    }()
    
    lazy var bDnaBtn:UIButton = {
        let bdb:UIButton = UIButton.init(type: UIButtonType.custom);
        let strAttr:NSAttributedString = NSAttributedString.init(string: "蓝", attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 18),
                                                                                             NSAttributedStringKey.foregroundColor:UIColor.white])
        let charAttr:NSAttributedString = NSAttributedString.init(string: "yB", attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 10),
                                                                                             NSAttributedStringKey.foregroundColor:UIColor.white])
        var attr:NSMutableAttributedString = NSMutableAttributedString.init(attributedString: strAttr);
        attr.append(charAttr);
        bdb.setAttributedTitle(attr, for: UIControlState.normal);
        bdb.addTarget(self, action:#selector(self.dnaAction(btn:)), for: UIControlEvents.touchUpInside);
        bdb.backgroundColor = UIColor.init(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0);
        bdb.layer.cornerRadius = 5;
        return bdb;
    }()
    
    lazy var yDnaBtn:UIButton = {
        let ydb:UIButton = UIButton.init(type: UIButtonType.custom);
        let strAttr:NSAttributedString = NSAttributedString.init(string: "黄", attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 18),
                                                                                             NSAttributedStringKey.foregroundColor:UIColor.white])
        let charAttr:NSAttributedString = NSAttributedString.init(string: "Yb", attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 10),
                                                                                             NSAttributedStringKey.foregroundColor:UIColor.white])
        var attr:NSMutableAttributedString = NSMutableAttributedString.init(attributedString: strAttr);
        attr.append(charAttr);
        ydb.setAttributedTitle(attr, for: UIControlState.normal);
        ydb.addTarget(self, action:#selector(self.dnaAction(btn:)), for: UIControlEvents.touchUpInside);
        ydb.backgroundColor = UIColor.init(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0);
        ydb.layer.cornerRadius = 5;
        return ydb;
    }()
    
    lazy var wDnaBtn:UIButton = {
        let wdb:UIButton = UIButton.init(type: UIButtonType.custom);
        let strAttr:NSAttributedString = NSAttributedString.init(string: "白", attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 18),
                                                                                             NSAttributedStringKey.foregroundColor:UIColor.white])
        let charAttr:NSAttributedString = NSAttributedString.init(string: "yb", attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 10),
                                                                                             NSAttributedStringKey.foregroundColor:UIColor.white])
        var attr:NSMutableAttributedString = NSMutableAttributedString.init(attributedString: strAttr);
        attr.append(charAttr);
        wdb.setAttributedTitle(attr, for: UIControlState.normal);
        wdb.addTarget(self, action:#selector(self.dnaAction(btn:)), for: UIControlEvents.touchUpInside);
        wdb.backgroundColor = UIColor.init(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0);
        wdb.layer.cornerRadius = 5;
        return wdb;
    }()
    
    lazy var acBtn:UIButton = {
        let acb:UIButton = UIButton.init(type: UIButtonType.custom);
        acb.setTitle("AC", for: UIControlState.normal);
        acb.backgroundColor = UIColor.orange;
        acb.addTarget(self, action:#selector(self.clearAction), for: UIControlEvents.touchUpInside);
        acb.layer.cornerRadius = 5;
        return acb;
    }()
    
    lazy var pairBtn:UIButton = {
        let pb:UIButton = UIButton.init(type: UIButtonType.custom);
        pb.setTitle("×", for: UIControlState.normal);
        pb.backgroundColor = UIColor.orange;
        pb.layer.cornerRadius = 5;
        pb.addTarget(self, action:#selector(self.pairAction), for: UIControlEvents.touchUpInside);
        return pb;
    }()
    
    lazy var resultBtn:UIButton = {
        let rb:UIButton = UIButton.init(type: UIButtonType.custom);
        rb.setTitle("＝", for: UIControlState.normal);
        rb.backgroundColor = UIColor.orange;
        rb.layer.cornerRadius = 5;
        rb.addTarget(self, action:#selector(self.resultAction), for: UIControlEvents.touchUpInside);
        return rb;
    }()
}
